#!/usr/bin/env python
import sys
import json
import random

from twisted.python import log
from twisted.internet import reactor

from autobahn.wamp import WampClientFactory, WampClientProtocol

TARGET = 'devytc'
HOST = 'localhost'
WS_PORT = 8081

class BroadcastClientProtocol(WampClientProtocol):
    def printEvent(self, topicUri, event):
        print '%(id)s -> %(payload)s' % event

    def sendSimpleEvent(self):
        print 'sending simple event'
        self.publish(TARGET, {
            'id': 'service-change', 
            'target': TARGET,
            'payload': 
                [
                    {
                        'uri': 'service://%s%s/%s' % (
                            TARGET,
                            random.choice(['01', '02']),
                            random.choice(['http', 'tomcat'])
                        ), 
                        'state': random.choice(['down', 'up'])
                    }
                ]
        })
        reactor.callLater(2, self.sendSimpleEvent)

    def sendComplexEvent(self, state=None, current=None, add_artefacts=False, **kwargs):
        print 'sending complex event'
        print add_artefacts
        if add_artefacts:
            self.publish(TARGET, {
                'id': 'full-update',
                'target': TARGET,
                'payload': 
                    [   # groups of hosts
                        [   # hosts
                            {
                                'uri': 'host://devytc01',
                                'name': 'devytc01',
                                'state': 'uptodate',
                                'services': [
                                    {'uri': 'service://devytc01/http', 'name': 'http', 'state': state},
                                    {'uri': 'service://devytc01/tomcat', 'name': 'tomcat', 'state': state},
                                    {'uri': 'service://devytc01/dummy', 'name': 'dummy', 'state': 'up'},
                                ],
                                'artefacts': [
                                    {'uri': 'artefact://devytc01/foo', 'name': 'foo', 'current': current},
                                    {'uri': 'artefact://devytc01/yadt-client', 'name': 'yadt-client', 'current': current},
                                    {'uri': 'artefact://devytc01/is24-repos', 'name': 'is24-repos', 'current': current}
                                ]
                            },
                            {
                                'uri': 'host://devytc02',
                                'name': 'devytc02',
                                'state': 'uptodate',
                                'services': [
                                    {'uri': 'service://devytc02/http', 'name': 'http', 'state': 'down'},
                                    {'uri': 'service://devytc02/tomcat', 'name': 'tomcat', 'state': 'up'},
                                ],
                                'artefacts': [
                                    {'uri': 'artefact://devytc02/foo', 'name': 'foo', 'current': current},
                                    {'uri': 'artefact://devytc02/bar', 'name': 'bar', 'current': 'aa0815'},
                                    {'uri': 'artefact://devytc02/yadt-client', 'name': 'yadt-client', 'current': current},
                                    {'uri': 'artefact://devytc02/is24-repos', 'name': 'is24-repos', 'current': current + '2'}
                                ]
                            }
                        ]
                    ]
            })
        else:
            self.publish(TARGET, {
            'id': 'full-update',
            'target': TARGET,
            'payload': 
                [   # groups of hosts
                    [   # hosts
                        {
                            'uri': 'host://devytc01',
                            'name': 'devytc01',
                            'state': 'uptodate',
                            'services': [
                                {'uri': 'service://devytc01/http', 'name': 'http', 'state': state},
                                {'uri': 'service://devytc01/tomcat', 'name': 'tomcat', 'state': state},
                                {'uri': 'service://devytc01/dummy', 'name': 'dummy', 'state': 'up'},
                            ],
                            'artefacts': [
                                {'uri': 'artefact://devytc01/yadt-client', 'name': 'yadt-client', 'current': current},
                                {'uri': 'artefact://devytc01/is24-repos', 'name': 'is24-repos', 'current': current}
                            ]
                        },
                        {
                            'uri': 'host://devytc02',
                            'name': 'devytc02',
                            'state': 'uptodate',
                            'services': [
                                {'uri': 'service://devytc02/http', 'name': 'http', 'state': 'down'},
                                {'uri': 'service://devytc02/tomcat', 'name': 'tomcat', 'state': 'up'},
                            ],
                            'artefacts': [
                                {'uri': 'artefact://devytc02/yadt-client', 'name': 'yadt-client', 'current': current},
                                {'uri': 'artefact://devytc02/is24-repos', 'name': 'is24-repos', 'current': current}
                            ]
                        }
                    ]
                ]
        })
        reactor.callLater(1, reactor.stop)
        #reactor.callLater(2, self.sendSimpleEvent)

    def onEvent1(self, topicUri, event):
        self.counter += 1
        self.publish('event:myevent2', {'trigger': event, 'counter': self.counter})

    def onSessionOpen(self):
        print 'onOpen'
        self.counter = 0
        #self.subscribe(TARGET, self.printEvent)
        if opts.force_client_reload:
            self.publish(TARGET, {'id': 'force-reload'})
        elif not opts.receive_only:
            self.sendComplexEvent(**vars(opts))

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-r', '--receive-only', action='store_true', dest='receive_only', default=False)
parser.add_option('-c', '--current', action='store', dest='current', default='foo') 
parser.add_option('-s', '--state', action='store', dest='state', default='up')
parser.add_option('-a', '--add-artefacts', action='store_true', dest='add_artefacts', default=False)
parser.add_option('', '--force-client-reload', action='store_true', dest='force_client_reload', default=False)
opts, args = parser.parse_args()

log.startLogging(sys.stdout)
factory = WampClientFactory('ws://%s:%s//' % (HOST, WS_PORT))
factory.protocol = BroadcastClientProtocol
reactor.connectTCP(HOST, WS_PORT, factory)
reactor.run()

